//
//  XZHeaderImageView.m
//  ChatDemo-UI2.0
//
//  Created by zz on 16/4/29.
//  Copyright © 2016年 zz. All rights reserved.
//

#import "XZHeaderImageView.h"

@implementation XZHeaderImageView{
    
    UIButton *btn;
}


-(instancetype)init{
    
    if (self = [super init]) {
        
        [self createView];
        
    }
    
    return self;
}

-(instancetype)initWithFrame:(CGRect)frame{
    
    if (self = [super initWithFrame:frame]) {
        
        [self createView];
    }
    
    return self;
}


-(void)createView{
    
    //头像
    _headImage = [[UIImageView alloc] init];
    _headImage.frame = self.bounds;
    _headImage.contentMode = UIViewContentModeScaleAspectFill;
    _headImage.clipsToBounds = YES;
    _headImage.layer.cornerRadius = self.frame.size.width / 2;
    [self addSubview:_headImage];
    
    //初始化namelabel
    
    if (self.nameStr.length > 2) {
        self.nameStr = [self.nameStr substringFromIndex:self.nameStr.length - 2];
    }
    _namelabel = [[UILabel alloc] init];
    _namelabel.font = [UIFont systemFontOfSize:14];
    _namelabel.textColor = [UIColor clearColor];
    _namelabel.backgroundColor=[UIColor clearColor];
    _namelabel.textAlignment = NSTextAlignmentCenter;
    [self addSubview:_namelabel];
    
    [self bringSubviewToFront:_namelabel];
    
    
    [_namelabel mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.centerX.equalTo(self.mas_centerX).offset(0);
        make.centerY.equalTo(self.mas_centerY).offset(0);
        make.width.equalTo(@30);
        make.height.equalTo(@15);
    }];
     
    
    //if (_headType != YES) {
    
        btn = [UIButton buttonWithType:(UIButtonTypeCustom)];
        [btn setFrame:CGRectMake(0, 0, self.width, self.height)];
        [btn addTarget:self action:@selector(headImageRecognizer) forControlEvents:(UIControlEventTouchUpInside)];
        [self addSubview:btn];
    //}
    

}

-(void)setHeadType:(BOOL)headType{
    
    [btn removeFromSuperview];
}


-(void)layoutSubviews{
    
    [super layoutSubviews];
    /*
    //头像
    _headImage = [[UIImageView alloc] init];
    _headImage.frame = self.bounds;
    _headImage.contentMode = UIViewContentModeScaleAspectFill;
    _headImage.layer.cornerRadius = self.frame.size.width / 2;
    [self addSubview:_headImage];
    
    //初始化namelabel
    _namelabel = [[UILabel alloc] init];
    _namelabel.font = [UIFont systemFontOfSize:10];
    _namelabel.textColor = [UIColor whiteColor];
    _namelabel.textAlignment = NSTextAlignmentCenter;
    [self addSubview:_namelabel];
    
    [self bringSubviewToFront:_namelabel];

    
    [_namelabel mas_makeConstraints:^(MASConstraintMaker *make) {
       
        make.centerX.equalTo(self.mas_centerX).offset(0);
        make.centerY.equalTo(self.mas_centerY).offset(0);
        make.width.equalTo(@30);
        make.height.equalTo(@15);
    }];
     */
}

- (void)setFlag:(BOOL)flag {
    
    if (flag) {
        
        _namelabel.hidden = YES;
    }else{
        _namelabel.hidden = NO;
    }

}

- (void)updateViewWithLogoStr:(NSString *)logoStr {
    
    
    ////头像
    //_headImage = [[UIImageView alloc] init];
    //_headImage.frame = self.bounds;
    //_headImage.contentMode = UIViewContentModeScaleAspectFill;
    //_headImage.layer.cornerRadius = self.frame.size.width / 2;
    //[self addSubview:_headImage];
    
    ////初始化namelabel
    //_namelabel = [[UILabel alloc] init];
    //_namelabel.font = [UIFont systemFontOfSize:10];
    //_namelabel.textColor = [UIColor whiteColor];
    //_namelabel.textAlignment = NSTextAlignmentCenter;
    //[self addSubview:_namelabel];
    
    //[self bringSubviewToFront:_namelabel];
    
    
    //[_namelabel mas_makeConstraints:^(MASConstraintMaker *make) {
    //make.centerX.equalTo(self.mas_centerX).offset(0);
    //make.centerY.equalTo(self.mas_centerY).offset(0);
    //make.width.equalTo(@30);
    //make.height.equalTo(@15);
    //}];
    
    if (logoStr && ![logoStr hasPrefix:@"http"]) {
        logoStr = [REQUESTIMAGEURL stringByAppendingString:logoStr];
    }
    
    [self.headImage sd_setImageWithURL:[NSURL URLWithString:logoStr]];
}

- (void)awakeFromNib{
    
    [super awakeFromNib];
    
    [self createView];
}

- (void)headImagePressed:(UIGestureRecognizer *)tap {
    
    NSLog(@"test ~");
}

- (void)headImageRecognizer {
    
    if (self.headPressed) {
        
        self.headPressed();
    }
}

@end
